package com.sgb.kjwl.view.ui.adapter.enterprise;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ItemPublishServiceBinding;
import com.sgb.kjwl.model.entity.enterprise.EpQuoteEntity;
import com.sgb.kjwl.utils.PointUtils;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.recyclerview.entity.BaseAreaEntity;
import com.swgk.core.util.DateUtils;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class PublishServiceAdapter extends BaseRecycleAdapter<EpQuoteEntity> {

    private String subStr;
    ItemPublishServiceBinding binding;

    public PublishServiceAdapter(Context context, List<EpQuoteEntity> datas) {
        super(context, R.layout.item_publish_service, datas);
    }

    @Override
    public void convert(ItemViewHolder holder, EpQuoteEntity epQuoteEntity) {
        holder.setBinding(BR.viewModel, epQuoteEntity);
        binding = (ItemPublishServiceBinding) holder.getmBinding();
//        binding.tvShangjia.setOnClickListener(v -> iCallPhone.call(epQuoteEntity.getId(), epQuoteEntity.getStatus()));
        binding.tvXiajia.setOnClickListener(v -> iCallPhone.call(Long.parseLong(epQuoteEntity.getId()), epQuoteEntity.getStatus()));
        binding.dateView.setText(DateUtils.timestampToDate(epQuoteEntity.getUpdateTime()));

        if (epQuoteEntity.getFirstCategoryName() != null && !epQuoteEntity.getFirstCategoryName().equals("")) {
            subStr = epQuoteEntity.getFirstCategoryName();
            if (epQuoteEntity.getSecondCategoryName() != null && !epQuoteEntity.getSecondCategoryName().equals("")) {
                String two = epQuoteEntity.getSecondCategoryName();
                subStr = subStr + "-" + two;
            }
            if (epQuoteEntity.getItemName() != null && !epQuoteEntity.getItemName().equals("")) {
                String three = epQuoteEntity.getItemName();
                subStr = subStr + "-" + three;
            }
        }
        binding.tvCategoryValue.setText(subStr);
        String cities = epQuoteEntity.getServiceCitys();
        if (cities != null && !cities.equals("")) {
            ArrayList<BaseAreaEntity> citysList = jsonToArrayList(cities, BaseAreaEntity.class);
            StringBuilder stringBuilder = new StringBuilder();
            for (BaseAreaEntity entity : citysList) {
                stringBuilder.append(entity.getName()).append("/");
            }
            String cityShow = stringBuilder.toString().substring(0, stringBuilder.toString().length() - 1);
            binding.tvServiceCity.setText(cityShow);
        } else {
            binding.tvServiceCity.setText("/");
        }

        binding.tvServiceStatus.setText(epQuoteEntity.getStatus().equals("1")?"上架":"下架");
        binding.tvXiajia.setText(epQuoteEntity.getStatus().equals("1")?"下架":"上架");
        binding.tvXiajia.setTextColor(epQuoteEntity.getStatus().equals("1")? Color.parseColor("#969696"):Color.parseColor("#FFCC00"));
        binding.tvXiajia.setBackgroundResource(epQuoteEntity.getStatus().equals("1")?R.drawable.corner_969696_bg_p_five:R.drawable.corner_yellow_bg_p_five);
        if(!TextUtils.isEmpty(epQuoteEntity.getDomesticCompaniesPrice())&&!TextUtils.isEmpty(epQuoteEntity.getMaximumPrice())){
            if(epQuoteEntity.getDomesticCompaniesPrice().equals("-1")) {
                binding.priceView.setText("议价");
            }else{
                binding.priceView.setText("¥"+ com.swgk.core.util.PointUtils.price(Double.parseDouble(epQuoteEntity.getDomesticCompaniesPrice()))+"起");
            }
            binding.llNumber.setVisibility(View.VISIBLE);
            binding.tvServiceNumber.setText(epQuoteEntity.getProjectClassificationCount()+"个");
        }else{
            binding.llNumber.setVisibility(View.VISIBLE);
            binding.tvServiceNumber.setText(1+"个");
            binding.priceView.setText(epQuoteEntity.getPrice()!=-1?TextViewBindingAdapter.setMoneyForRMB("¥"+ com.swgk.core.util.PointUtils.price(epQuoteEntity.getPrice())):"议价");
        }


    }

    /**
     * @param json
     * @param clazz
     * @return
     */
    public static <T> ArrayList<T> jsonToArrayList(String json, Class<T> clazz) {
        Type type = new TypeToken<ArrayList<JsonObject>>() {
        }.getType();
        ArrayList<T> arrayList = new ArrayList<>();
        try {
            ArrayList<JsonObject> jsonObjects = new Gson().fromJson(json, type);

            for (JsonObject jsonObject : jsonObjects) {
                arrayList.add(new Gson().fromJson(jsonObject, clazz));
            }
        } catch (Exception e) {

        }

        return arrayList;
    }

    private ICallPhone iCallPhone;

    public interface ICallPhone {
        void call(long id, String status);
    }

    public void OnCallPhone(ICallPhone iCallPhone) {
        this.iCallPhone = iCallPhone;
    }
}
